<?PHP
/*
 * $Id: FileuploadAction.class.php 162 2009-12-16 16:03:27Z namba0219 $
 * =============================================================================
 * Barge - Web application framework on PHP5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace examples\actions;

use barge\System;
use barge\io\File;
use barge\utils\ArrayList;
use barge\utils\HashMap;
use barge\utils\FileUtils;
use barge\utils\OutputBuffer;
use barge\utils\String;
use barge\utils\Timestamp;
use barge\web\ActionContext;
use barge\web\DispatchAction;
use barge\web\Request;
use barge\web\Response;
use barge\web\Messages;
use barge\web\Message;
use barge\web\Validator;



/**
 *
 * @version 1.0 2009/01/24
 * @author Kenji Namba
 * @see DispatchAction
 */
class FileuploadAction extends DispatchAction
{
	/**
	 * @param ActionContext $context
	 * @see Action#execute
	 */
	public function init(ActionContext $context)
	{
//var_dump($_FILES);
		$context->render();
	}

	/**
	 * @param ActionContext $context
	 * @see Action#execute
	 */
	public function upload(ActionContext $context)
	{
		/* @var $form FileuploadForm */
		$form = $context->getForm(false);

		// 入力チェック
		$messages = $this->validateUpload($form);
		if ($messages->hasElement()) {
			$context->saveMessages($messages);
			$context->render();
			return;
		}

		// ワークディレクトリーにコピー
		$workdir = $context->getApplication()->getWorkDir();
		File::makeDir($workdir. '/uploads');

		$form->getImage()->moveTo($file, '/sample.jpg');
		$form->getImage()->destroy();

		$context->render();
	}


	/**
	 * @return ArrayList
	 */
	public function listImages()
	{
	}

	/**
	 * @param FileuploadForm form
	 * @return Messages
	 */
	protected function validateUpload(FileuploadForm $form)
	{
		$messages = new Messages();
		if ($form->getImage() === null) {
			$messages->add(new Message('image', 'errors.selected', 'イメージ'));
		} else if (!$this->validateFile($form)) {
			$messages->add(new Message('image', 'errors.filetype', 'gif,jpg 画像'));
		}
		return $messages;
	}

	/**
	 * @param FileuploadForm $form
	 */
	protected function validateFile(FileuploadForm $form)
	{
		$ext = FileUtils::getExtension($form->getImage()->getFileName());
		return Validator::isWithin($ext, array('gif', 'jpg'));
	}

	/**
	 * @return ArrayList
	 */
	protected function getMethods()
	{
		return new ArrayList('upload');
	}

}
